/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class HaLouPostJson extends BaseJsonAdapter{

    async doRequest(){
        var form = {
          action: 'user.guide.getNews'
        }

        var formData = JSON.stringify(form);
        var contentLength = formData.length;

        var options = {
            headers:{
              'Accept': 'application/json, text/javascript, */*; q=0.01',
              'Content-Type': 'text/plain;charset=UTF-8',
              'Origin': 'http://www.hellobike.com',
              'Referer': 'http://www.hellobike.com/news.html',
            },
            // uri:'http://www.bjp2p.com.cn/supervise/queryNews4List',
            uri:'https://api.ttbike.com.cn/api?user.guide.getNews',
            body:formData,
            method:"POST",
            encoding:null
        }
        let body = await request(options)
        return body;
    }


    async reqJson(){
		    const body = await this.doRequest();
        let decodeBody = iconv.decode(body, this.encoding).toString();
        let data = JSON.parse(decodeBody);
        let url = 'http://www.hellobike.com/newsDetail.html?guid='
        let contents = [];
        for(let row of data.data) {
          for(let info of row.articleList) {
            contents.push({
              title: info['title'],
              href: url + info['guid']
            });
          }
        }
        return {data:contents}
    }
}
